package list;

import java.util.HashSet;
import java.util.Set;

public class HasCycle {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);

        l1.next = l2;
        l2.next = l3;
        // l3.next = l1;

        boolean ret = hasCycle(l1);
        System.out.println(ret);
    }

    public static boolean hasCycle(ListNode head) {
        Set<ListNode> content = new HashSet<>();
        while (head != null) {
            if (!content.add(head)) {
                return true;
            }

            head = head.next;
        }

        return false;
    }

}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
        next = null;
    }
}
